﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;
using log4net;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.ApiService.Controllers
{
    /// <summary>
    /// 用户相关API
    /// </summary>
    public class ConsumerApiController : BaseApiController
    {
        private readonly ILog _fileLog = LogManager.GetLogger(typeof(ConsumerApiController));
        private readonly ConsumerBLL _consumerBll = new ConsumerBLL();
        private readonly ConsumerAddressBLL _consumerAddressBll = new ConsumerAddressBLL();
        private readonly CouponBLL _couponBll = new CouponBLL();
        private readonly ShopCommodityBLL _shopCommodityBll = new ShopCommodityBLL();
        private readonly CommodityLibBLL _commodityLibBll = new CommodityLibBLL();

        //POST api/ConsumerApi/GetFullConsumerInfo
        /// <summary>
        /// 根据McId以及OpenId获取用户信息
        /// </summary>
        /// <param name="mcId">码云卡商户号</param>
        /// <param name="openId">用户OPENID</param>
        /// <returns>返回消费者信息（含关联的微信用户信息）</returns>
        [HttpPost]
        public Result<ConsumerDto> GetFullConsumerInfo(string mcId, string openId)
        {
            var checkResult = CheckPermission("ConsumerApiController | GetFullConsumerInfo");
            if (!checkResult.Status)
            {
                return new Result<ConsumerDto>
                {
                    Status = checkResult.Status,
                    Message = checkResult.Message,
                    Data = null
                };
            }

            if (string.IsNullOrWhiteSpace(mcId) || string.IsNullOrWhiteSpace(openId))
            {
                _fileLog.Error("ConsumerApiController | GetFullConsumerInfo | Error: Empty paramter.");
                return new Result<ConsumerDto>
                {
                    Data = null,
                    Message = "Empty paramter",
                    Status = false
                };
            }

            try
            {
                var wechatUserInfo = GetMxApiWechatUserInfo(openId, mcId);
                if (wechatUserInfo == null || wechatUserInfo.Id == 0)
                {
                    _fileLog.Error(
                        "ConsumerApiController | GetFullConsumerInfo | Cannot find the wechat user info from database. OpenId - " +
                        openId + " McId - " + mcId);
                    return new Result<ConsumerDto>
                    {
                        Status = false,
                        Message =
                            "Cannot find the wechat user info from database. OpenId - " + openId + " McId - " + mcId,
                        Data = null
                    };
                }

                var consumerInfo = _consumerBll.GetConsumerInfoByMxWechatInfoUserId(wechatUserInfo.WiuId);
                if (consumerInfo == null || consumerInfo.Id == 0)
                {
                    _fileLog.Error(
                        "ConsumerApiController | GetFullConsumerInfo | Cannot find the consumer user info from database. OpenId - " +
                        openId + " McId - " + mcId);
                    return new Result<ConsumerDto>
                    {
                        Status = false,
                        Message =
                            "Cannot find the consumer user info from database. OpenId - " + openId + " McId - " + mcId,
                        Data = null
                    };
                }
                
                return new Result<ConsumerDto>
                {
                    Status = true,
                    Message = "Success",
                    Data = new ConsumerDto
                    {
                        BaseConsumerInfo = consumerInfo,
                        RelatedWechatUserInfo = wechatUserInfo
                    }
                };
            }
            catch (Exception ex)
            {
                _fileLog.Error("ConsumerApiController | GetFullConsumerInfo | Exception: " + ex.Message + ex.StackTrace);
                return new Result<ConsumerDto>
                {
                    Status = false,
                    Message = ex.Message,
                    Data = null
                };
            }
        }

        // POST api/ConsumerApi/GetConsumerAddress/
        /// <summary>
        /// 获取用户的收获地址
        /// </summary>
        /// <param name="consumerId">消费者ID</param>
        /// <returns>返回消费者送货地址列表</returns>
        [HttpPost]
        public Result<List<ConsumerAddress>> GetConsumerAddress(int consumerId)
        {
            var checkResult = CheckPermission("ConsumerApiController | GetConsumerAddress");
            if (!checkResult.Status)
            {
                return new Result<List<ConsumerAddress>>
                {
                    Status = checkResult.Status,
                    Message = checkResult.Message,
                    Data = null
                };
            }

            if (consumerId <= 0)
            {
                _fileLog.Error("ConsumerApiController | GetConsumerAddress | Error: Empty paramter.");
                return new Result<List<ConsumerAddress>>
                {
                    Data = null,
                    Message = "Empty paramter",
                    Status = false
                };
            }

            try
            {
                var addressList = _consumerAddressBll.GetModelListByConsumerId(consumerId);
                return new Result<List<ConsumerAddress>>
                {
                    Status = true,
                    Message = "Success",
                    Data = addressList
                };
            }
            catch (Exception ex)
            {
                _fileLog.Error("ConsumerApiController | GetConsumerAddress | Exception: " + ex.Message + ex.StackTrace);
                return new Result<List<ConsumerAddress>>
                {
                    Status = false,
                    Message = ex.Message,
                    Data = null
                };
            }

        }

        // POST api/ConsumerApi/GetConsumerAddressInfoById/
        /// <summary>
        /// 通过用户ID和用户地址薄ID获取地址信息
        /// </summary>
        /// <param name="cAddId">地址薄ID</param>
        /// <param name="consumerId">用户ID</param>
        /// <returns>返回地址信息</returns>
        [HttpPost]
        public Result<ConsumerAddress> GetConsumerAddressInfoById(int cAddId, int consumerId)
        {
            var checkResult = CheckPermission("ConsumerApiController | GetConsumerAddressInfoById");
            if (!checkResult.Status)
            {
                return new Result<ConsumerAddress>
                {
                    Status = checkResult.Status,
                    Message = checkResult.Message,
                    Data = null
                };
            }

            if (consumerId <= 0 || cAddId <= 0)
            {
                _fileLog.Error("ConsumerApiController | GetConsumerAddressInfoById | Error: Empty paramter.");
                return new Result<ConsumerAddress>
                {
                    Status = false,
                    Message = "Empty parameter",
                    Data = null
                };
            }

            try
            {
                var addressInfo = _consumerAddressBll.GetModel(cAddId);
                if (addressInfo == null || addressInfo.Id == 0 || addressInfo.ConsumerId != consumerId)
                {
                    return new Result<ConsumerAddress>
                    {
                        Status = false,
                        Message = "Cannot find the consumer's address info",
                        Data = null
                    };
                }

                return new Result<ConsumerAddress>
                {
                    Status = true,
                    Message = "Success",
                    Data = addressInfo
                };
            }
            catch (Exception ex)
            {
                _fileLog.Error("ConsumerApiController | GetConsumerAddressInfoById | Exception: " + ex.Message + ex.StackTrace);
                return new Result<ConsumerAddress>
                {
                    Status = false,
                    Message = ex.Message,
                    Data = null
                };
            }
        }

        // POST api/ConsumerApi/SaveConsumerAddress/
        /// <summary>
        /// 保存用户的收获地址
        /// </summary>
        /// <param name="addressInfoEncode">保存的地址信息对象加密字符串</param>
        /// <returns>返回新增记录的ID值</returns>
        [HttpPost]
        public Result<int> SaveConsumerAddress(string addressInfoEncode)
        {
            var checkResult = CheckPermission("ConsumerApiController | SaveConsumerAddress");
            if (!checkResult.Status)
            {
                return new Result<int>
                {
                    Status = checkResult.Status,
                    Message = checkResult.Message,
                    Data = 0
                };
            }

            if (string.IsNullOrWhiteSpace(addressInfoEncode))
            {
                _fileLog.Error("ConsumerApiController | SaveConsumerAddress | Error: Empty paramter.");
                return new Result<int>
                {
                    Status = false,
                    Message = "Empty paramter",
                    Data = 0
                };
            }

            try
            {
                var strAddressDecode = EncryptionLib.Decrypt(addressInfoEncode, Encoding.UTF8);
                if (string.IsNullOrWhiteSpace(strAddressDecode))
                {
                    return new Result<int>
                    {
                        Status = false,
                        Message = "Invalid encoded order info json",
                        Data = 0
                    };
                }

                //反序列化订单JSON到对象
                var addressInfo = JsonTool.JsonToObj<ConsumerAddress>(strAddressDecode);

                if (addressInfo == null || addressInfo.AreaId <= 0 || string.IsNullOrWhiteSpace(addressInfo.Address) ||
                string.IsNullOrWhiteSpace(addressInfo.LinkMan) || string.IsNullOrWhiteSpace(addressInfo.LinkMobile) ||
                string.IsNullOrWhiteSpace(addressInfo.Latitude) || string.IsNullOrWhiteSpace(addressInfo.Longitude))
                {
                    _fileLog.Error("ConsumerApiController | SaveConsumerAddress | Error: INvalid paramter.");
                    return new Result<int>
                    {
                        Data = 0,
                        Message = "Empty paramter",
                        Status = false
                    };
                }
                //var addressList = _consumerAddressBll.GetModelList(string.Format("Address='{0}'", addressInfo.Address)).FirstOrDefault();
                //if (addressList != null && addressList.Id > 0)
                //{
                //    return new Result<int>
                //    {
                //        Status = false,
                //        Message = "Address already exist",
                //        Data = 0
                //    };
                //}
                if (addressInfo.IsDefault)
                {
                    ConsumerAddressBLL.Instance.DeleteDefaultAddress(addressInfo.ConsumerId);
                }
                var nNewRet = _consumerAddressBll.Add(addressInfo);
                return new Result<int>
                {
                    Status = true,
                    Message = "Success",
                    Data = nNewRet
                };
            }
            catch (Exception ex)
            {
                _fileLog.Error("ConsumerApiController | SaveConsumerAddress | Exception: " + ex.Message + ex.StackTrace);
                return new Result<int>
                {
                    Status = false,
                    Message = ex.Message,
                    Data = 0
                };
            }

        }

        // POST api/ConsumerApi/ModifyConsumerAddress/
        /// <summary>
        /// 修改用户的收获地址
        /// </summary>
        /// <param name="addressInfoEncode">保存的地址信息对象加密字符串</param>
        /// <returns>返回修改地址记录ID</returns>
        [HttpPost]
        public Result<int> ModifyConsumerAddress(string addressInfoEncode)
        {
            var checkResult = CheckPermission("ConsumerApiController | ModifyConsumerAddress");
            if (!checkResult.Status)
            {
                return new Result<int>
                {
                    Status = checkResult.Status,
                    Message = checkResult.Message,
                    Data = 0
                };
            }

            if (string.IsNullOrWhiteSpace(addressInfoEncode))
            {
                _fileLog.Error("ConsumerApiController | ModifyConsumerAddress | Error: Empty paramter.");
                return new Result<int>
                {
                    Status = false,
                    Message = "Empty paramter",
                    Data = 0
                };
            }

            try
            {
                var strAddressDecode = EncryptionLib.Decrypt(addressInfoEncode, Encoding.UTF8);
                if (string.IsNullOrWhiteSpace(strAddressDecode))
                {
                    return new Result<int>
                    {
                        Status = false,
                        Message = "Invalid encoded order info json",
                        Data = 0
                    };
                }

                //反序列化订单JSON到对象
                var addressInfo = JsonTool.JsonToObj<ConsumerAddress>(strAddressDecode);

                if (addressInfo == null || addressInfo.Id <= 0 || addressInfo.AreaId <= 0 || string.IsNullOrWhiteSpace(addressInfo.Address) ||
                string.IsNullOrWhiteSpace(addressInfo.LinkMan) || string.IsNullOrWhiteSpace(addressInfo.LinkMobile) ||
                string.IsNullOrWhiteSpace(addressInfo.Latitude) || string.IsNullOrWhiteSpace(addressInfo.Longitude))
                {
                    _fileLog.Error("ConsumerApiController | ModifyConsumerAddress | Error: INvalid paramter.");
                    return new Result<int>
                    {
                        Data = 0,
                        Message = "Empty paramter",
                        Status = false
                    };
                }
                if (addressInfo.IsDefault)
                {
                    ConsumerAddressBLL.Instance.DeleteDefaultAddress(addressInfo.ConsumerId);
                }
                var bRet = _consumerAddressBll.Update(addressInfo);
                return new Result<int>
                {
                    Status = bRet,
                    Message = bRet ? "Success" : "FALSE",
                    Data = bRet ? addressInfo.Id : 0
                };
            }
            catch (Exception ex)
            {
                _fileLog.Error("ConsumerApiController | ModifyConsumerAddress | Exception: " + ex.Message + ex.StackTrace);
                return new Result<int>
                {
                    Status = false,
                    Message = ex.Message,
                    Data = 0
                };
            }

        }

        // POST api/ConsumerApi/GetConsumerConponInfoById/
        /// <summary>
        /// 通过用户优惠券ID获取优惠券信息
        /// </summary>
        /// <param name="consumerCouponId">优惠券ID</param>
        /// <returns>返回优惠券信息</returns>
        [HttpPost]
        public Result<CouponDto> GetConsumerConponInfoById(int consumerCouponId)
        {
            var checkResult = CheckPermission("ConsumerApiController | GetConsumerConponInfoById");
            if (!checkResult.Status)
            {
                return new Result<CouponDto>
                {
                    Status = checkResult.Status,
                    Message = checkResult.Message,
                    Data = null
                };
            }

            if (consumerCouponId <= 0)
            {
                _fileLog.Error("ConsumerApiController | GetConsumerConponInfoById | Error: Empty paramter.");
                return new Result<CouponDto>
                {
                    Status = false,
                    Message = "Empty parameter",
                    Data = null
                };
            }

            try
            {
                var couponDtoInfo = _couponBll.GetCouponInfoByConsumerCouponId(consumerCouponId);
                if (couponDtoInfo == null || couponDtoInfo.CouponInfo == null || couponDtoInfo.CouponInfo.Id == 0)
                {
                    return new Result<CouponDto>
                    {
                        Status = false,
                        Message = "Cannot find the record from database",
                        Data = null
                    };
                }

                return new Result<CouponDto>
                {
                    Status = true,
                    Message = "Success",
                    Data = couponDtoInfo
                };
            }
            catch (Exception ex)
            {
                _fileLog.Error("ConsumerApiController | GetConsumerConponInfoById | Exception: " + ex.Message + ex.StackTrace);
                return new Result<CouponDto>
                {
                    Status = false,
                    Message = ex.Message,
                    Data = null
                };
            }
        }

        // POST api/ConsumerApi/GetUsableCouponListOfConsumer/
        /// <summary>
        /// 获取用户可用优惠券列表
        /// </summary>
        /// <param name="consumerId">用户ID</param>
        /// <param name="verifyCode">校验码</param>
        /// <param name="shopId">店铺ID（可选 ）</param>
        /// <param name="shopCommodityIds">店铺商品ID集合（可选）</param>
        /// <returns>返回用户持有的有效优惠券列表</returns>
        [HttpPost]
        public Result<List<CouponDto>> GetUsableCouponListOfConsumer(int consumerId, string verifyCode,
            string shopCommodityIds = "", int shopId = 0)
        {
            var checkResult = CheckPermission("ConsumerApiController | GetUsableCouponListOfConsumer");
            if (!checkResult.Status)
            {
                return new Result<List<CouponDto>>
                {
                    Status = checkResult.Status,
                    Message = checkResult.Message,
                    Data = null
                };
            }

            if (consumerId <= 0)
            {
                _fileLog.Error("ConsumerApiController | GetUsableCouponListOfConsumer | Error: Empty paramter.");
                return new Result<List<CouponDto>>
                {
                    Data = null,
                    Message = "Empty paramter",
                    Status = false
                };
            }

            try
            {
                //校验参数数据
                var strSouceToEncode = consumerId + shopCommodityIds + shopId;
                var strLocalEncodeResult = EncryptionLib.Encrypt(strSouceToEncode, Encoding.UTF8);
                if (strLocalEncodeResult != verifyCode)
                {
                    _fileLog.Error("ConsumerApiController | GetUsableCouponListOfConsumer | Error: Invalid verify code.");
                    return new Result<List<CouponDto>>
                    {
                        Status = false,
                        Message = "Invalid verify code",
                        Data = null
                    };
                }

                List<CommodityLib> lsCommodityLib = null;
                if (shopId > 0 && !string.IsNullOrWhiteSpace(shopCommodityIds))
                {
                    lsCommodityLib = _commodityLibBll.GetCommodityInfoByAppointedShopCommodityIds(shopCommodityIds);
                    if (lsCommodityLib == null || lsCommodityLib.Count == 0)
                    {
                        return new Result<List<CouponDto>>
                        {
                            Status = false,
                            Message = "Cannot find the commodity info in database.Step 1",
                            Data = null
                        };
                    }
                }

                var lsCouponDto = _couponBll.GetCouponListOfCousumer(consumerId, true, lsCommodityLib);
                if (lsCouponDto == null || lsCouponDto.Count == 0)
                {
                    return new Result<List<CouponDto>>
                    {
                        Data = null,
                        Message = "Cannot find the records from database",
                        Status = false
                    };
                }
                return new Result<List<CouponDto>>
                {
                    Status = true,
                    Message = "Success",
                    Data = lsCouponDto
                };

            }
            catch (Exception ex)
            {
                _fileLog.Error("ConsumerApiController | GetUsableCouponListOfConsumer | Exception: " + ex.Message +
                               ex.StackTrace);
                return new Result<List<CouponDto>>
                {
                    Status = false,
                    Message = ex.Message,
                    Data = null
                };
            }
        }

        // POST api/ConsumerApi/BatchGetConsumerConponInfoById/
        /// <summary>
        /// 批量通过用户优惠券ID获取优惠券信息
        /// </summary>
        /// <param name="consumerCouponIds">优惠券ID</param>
        /// <param name="verifyCode"></param>
        /// <returns>返回优惠券信息</returns>
        [HttpPost]
        public Result<List<CouponDto>> BatchGetConsumerConponInfoById(string consumerCouponIds, string verifyCode)
        {
            var checkResult = CheckPermission("ConsumerApiController | BatchGetConsumerConponInfoById");
            if (!checkResult.Status)
            {
                return new Result<List<CouponDto>>
                {
                    Status = checkResult.Status,
                    Message = checkResult.Message,
                    Data = null
                };
            }

            if (string.IsNullOrWhiteSpace(consumerCouponIds) || string.IsNullOrWhiteSpace(verifyCode))
            {
                _fileLog.Error("ConsumerApiController | BatchGetConsumerConponInfoById | Error: Empty paramter.");
                return new Result<List<CouponDto>>
                {
                    Status = false,
                    Message = "Empty parameter",
                    Data = null
                };
            }

            try
            {
                //校验参数数据
                var strLocalEncodeResult = EncryptionLib.Encrypt(consumerCouponIds, Encoding.UTF8);
                if (strLocalEncodeResult != verifyCode)
                {
                    _fileLog.Error("ConsumerApiController | BatchGetConsumerConponInfoById | Error: Invalid verify code.");
                    return new Result<List<CouponDto>>
                    {
                        Status = false,
                        Message = "Invalid verify code",
                        Data = null
                    };
                }

                var lsCouponDtoInfo = _couponBll.BatchGetCouponInfoByConsumerCouponIds(consumerCouponIds);
                if (lsCouponDtoInfo == null || lsCouponDtoInfo.Count <= 0)
                {
                    return new Result<List<CouponDto>>
                    {
                        Status = false,
                        Message = "Cannot find the record from database",
                        Data = null
                    };
                }

                return new Result<List<CouponDto>>
                {
                    Status = true,
                    Message = "Success",
                    Data = lsCouponDtoInfo
                };
            }
            catch (Exception ex)
            {
                _fileLog.Error("ConsumerApiController | BatchGetConsumerConponInfoById | Exception: " + ex.Message + ex.StackTrace);
                return new Result<List<CouponDto>>
                {
                    Status = false,
                    Message = ex.Message,
                    Data = null
                };
            }
        }

        // POST api/ConsumerApi/GetConsumerShopId/
        /// <summary>
        /// 获取用户应进入的店铺ID
        /// </summary>
        /// <param name="consumerId">用户Id</param>
        /// <param name="verifyCode">加密字符串</param>
        /// <param name="shopTel">店铺电话</param>
        /// <param name="consumerMobile">客户电话</param>
        /// <param name="consumerAddress">客户地址</param>
        /// <returns>返回应进入的店铺ID</returns>
        [HttpPost]
        public Result<int> GetConsumerShopId(int consumerId, string verifyCode, string shopTel = null, string consumerMobile = null, string consumerAddress = null)
        {
            var result = new Result<int>();
            try
            {
                #region 合法性校验
                var checkResult = CheckPermission("ConsumerApiController | GetConsumerShopId");
                if (!checkResult.Status)
                {
                    result.Message = checkResult.Message;
                    return result;
                }

                if (consumerId <= 0 || string.IsNullOrEmpty(verifyCode))
                {
                    result.Message = "Empty paramter";
                    return result;
                }

                var checkVerifyCode = EncryptionLib.Encrypt(consumerId + shopTel + consumerMobile + consumerAddress);
                if (checkVerifyCode != verifyCode)
                {
                    _fileLog.Error("ConsumerApiController | GetConsumerShopId | Error: Invalid verify code.");
                    result.Message = "Invalid verify code";
                    return result;
                }
                #endregion

                #region 根据用户绑定店铺或最后一次购买的店铺进行匹配
                var consumer = ConsumerBLL.Instance.GetModel(consumerId);
                if (consumer == null)
                {
                    result.Message = "can not find user";
                    return result;
                }
                Shops shop;
                if (consumer.ShopId > 0)//用户已绑定店铺
                {
                    shop = ShopsBLL.Instance.GetModel(consumer.ShopId);
                    if (shop != null)
                    {
                        result.Status = true;
                        result.Data = shop.Id;
                        return result;
                    }
                }
                var consumerShopList = ShopsBLL.Instance.GetShopListOfConsumerVisited(consumer.Id);
                if (consumerShopList != null && consumerShopList.Count > 0)//用户有购买记录
                {
                    result.Status = true;
                    result.Data = consumerShopList[0].Id;
                    return result;
                }
                #endregion

                #region 用户初次进入系统输入原订水电话匹配店铺并绑定
                if (!string.IsNullOrEmpty(shopTel))
                {
                    shop = ShopsBLL.Instance.GetModelByTel(shopTel);
                    if (shop != null)
                    {
                        var mcAccount = McAccountsBLL.Instance.GetModelList("ShopId=" + shop.Id + " and AccountType=1").FirstOrDefault();
                        if (mcAccount != null)
                        {
                            consumer.ShopId = shop.Id;
                            consumer.ShopAccountId = mcAccount.Id;
                            ConsumerBLL.Instance.Update(consumer);
                        }
                        result.Status = true;
                        result.Data = shop.Id;
                        return result;
                    }
                }
                #endregion

                #region 用户初次进入系统输入自己的电话匹配店铺并绑定
                if (!string.IsNullOrEmpty(consumerMobile))
                {
                    shop = OldShopConsumerInfoBLL.Instance.GetShopByOldConsumerInfo(consumerMobile: consumerMobile);
                    if (shop != null)
                    {
                        var mcAccount = McAccountsBLL.Instance.GetModelList("ShopId=" + shop.Id + " and AccountType=1").FirstOrDefault();
                        if (mcAccount != null)
                        {
                            consumer.ShopId = shop.Id;
                            consumer.ShopAccountId = mcAccount.Id;
                            ConsumerBLL.Instance.Update(consumer);
                        }
                        result.Status = true;
                        result.Data = shop.Id;
                        return result;
                    }
                }
                #endregion

                #region 用户初次进入系统输入自己的地址匹配店铺并绑定
                if (!string.IsNullOrEmpty(consumerAddress))
                {
                    shop = OldShopConsumerInfoBLL.Instance.GetShopByOldConsumerInfo(consumerAddress: consumerAddress);
                    if (shop != null)
                    {
                        var mcAccount = McAccountsBLL.Instance.GetModelList("ShopId=" + shop.Id + " and AccountType=1").FirstOrDefault();
                        if (mcAccount != null)
                        {
                            consumer.ShopId = shop.Id;
                            consumer.ShopAccountId = mcAccount.Id;
                            ConsumerBLL.Instance.Update(consumer);
                        }
                        result.Status = true;
                        result.Data = shop.Id;
                        return result;
                    }
                }
                #endregion

                result.Status = true;
                result.Data = 0;
                return result;
            }
            catch (Exception ex)
            {
                _fileLog.Error("ConsumerApiController | GetConsumerShopId ：" + ex);
                result.Message = ex.Message;
                return result;
            }
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Result<Consumer> ModifyUserInfo(string textJson)
        {
            #region 合法性校验
            var checkResult = CheckPermission("ConsumerApiController | ModifyUserInfo");
            if (!checkResult.Status)
            {
                return new Result<Consumer>
                {
                    Status = checkResult.Status,
                    Message = checkResult.Message,
                    Data = null
                };
            }
            textJson = EncryptionLib.Decrypt(textJson, Encoding.UTF8);
            if (string.IsNullOrWhiteSpace(textJson))
            {
                return new Result<Consumer>
                {
                    Status = false,
                    Message = "Invalid encoded order info json",
                    Data = null
                };
            }

            //反序列化订单JSON到对象
            var consumerJS = JsonTool.JsonToObj<Consumer>(textJson);
            if (consumerJS.Id <= 0)
            {
                return new Result<Consumer>
                {
                    Status = checkResult.Status,
                    Message = "Parameters cannot be empty",
                    Data = null
                };
            }
            #endregion
            try
            {
                var consumer = ConsumerBLL.Instance.GetModel(consumerJS.Id);
                if (consumer == null)
                {
                    _fileLog.Error("UserController | ModifyUserInfo | 用户未授权登录 ");
                    return new Result<Consumer>
                    {
                        Status = false,
                        Message = "exist user is not",
                        Data = null
                    };
                }
                if (!string.IsNullOrEmpty(consumerJS.Birth.ToString()))
                { 
                consumer.Birth = consumerJS.Birth;
                }
                if (!string.IsNullOrEmpty(consumerJS.Email.ToString()))
                {
                    consumer.Email = consumerJS.Email;
                }
                if (!string.IsNullOrEmpty(consumerJS.Mobile.ToString()))
                    {
                        consumer.Mobile = consumerJS.Mobile;
                    }
                    if (!string.IsNullOrEmpty(consumerJS.NickName.ToString()))
                    {
                        consumer.NickName = consumerJS.NickName;
                    }
                    if (!string.IsNullOrEmpty(consumerJS.Sex.ToString()))
                    {
                        consumer.Sex = consumerJS.Sex;
                    }
                    if (!string.IsNullOrEmpty(consumerJS.RealName.ToString()))
                    {
                        consumer.RealName = consumerJS.RealName;
                    }

                    if (!string.IsNullOrEmpty(consumerJS.WeChatAccount))
                {
                    var localProjectInfo = MatrixCloudProjectInfoBLL.Instance.GetModel(consumer.MxProjectInfoId);
                    if (localProjectInfo == null || localProjectInfo.Id == 0)
                    {
                        _fileLog.Error(
                            "UserController | ModifyUserInfo | No MatrixCloudProjectInfo info info in dababase | nMxProjectInfoId : " +
                            consumer.MxProjectInfoId);
                        return new Result<Consumer>
                        {
                            Status = false,
                            Message = "No MatrixCloudProjectInfo info",
                            Data = null
                        };
                    }

                    var resultProjectInfo =
                        MatrixCloudCenterInterFuncBllObj.CheckAndGetMatrixCloudProjectInfo(localProjectInfo.PjCode);
                    if (!resultProjectInfo.Status)
                    {
                        _fileLog.Error(
                           "OrderExtBll | SendOrderWeChatTemplateMsg | No MatrixCloudProjectInfo info info in Matrix Cloud Center | nMxProjectInfoId : " +
                           consumer.MxProjectInfoId + " " + resultProjectInfo.Message);
                        return new Result<Consumer>
                        {
                            Status = false,
                            Message = "No MatrixCloudProjectInfo info info in Matrix Cloud Center",
                            Data = null
                        };
                    }

                    var resultWechatUser =
                        MatrixCloudCenterInterFuncBllObj.GetWechatUserInfoByMatrixWechatUserInfoId(
                            consumer.MxWechatInfoUserId, resultProjectInfo.Data.PjMcId, resultProjectInfo.Data.PjSecret);
                    if (!resultWechatUser.Status)
                    {
                        return new Result<Consumer>
                        {
                            Status = false,
                            Message = resultWechatUser.Message,
                            Data = null
                        }; 
                    }

                    if (resultWechatUser.Data != null)
                    {
                        resultWechatUser.Data.WeChatAccount = consumerJS.WeChatAccount;
                        resultWechatUser.Data.RealName = consumerJS.RealName;
                        MatrixCloudCenterInterFuncBllObj.UpdateWechatUserBaseInfo(resultWechatUser.Data,
                            resultProjectInfo.Data.PjMcId, resultProjectInfo.Data.PjSecret);
                    }
                }

                if (ConsumerBLL.Instance.Update(consumer))
                {
                    return new Result<Consumer>
                    {
                        Status = true,
                        Message = "Success",
                        Data = consumer
                    };
                }
                return new Result<Consumer>
                {
                    Status = false,
                    Message = "修改失败",
                    Data = null
                };
            }
            catch (Exception ex)
            {
                return new Result<Consumer>
           {
               Status = false,
               Message = ex.Message,
               Data = null
           };
            }
        }



    }
}
